Data types in R

Data structures in R

Atomic vector

x <- 100

Atomic vectors with multiple values

x <- c("apple", "banana", "mango")
y <- c(12, 45, 87, 90)
z <- 1:100

Using variables in operations

y * 5
## [1]  60 225 435 450
z + y
##   [1]  13  47  90  94  17  51  94  98  21  55  98 102  25  59 102 106  29  63
##  [19] 106 110  33  67 110 114  37  71 114 118  41  75 118 122  45  79 122 126
##  [37]  49  83 126 130  53  87 130 134  57  91 134 138  61  95 138 142  65  99
##  [55] 142 146  69 103 146 150  73 107 150 154  77 111 154 158  81 115 158 162
##  [73]  85 119 162 166  89 123 166 170  93 127 170 174  97 131 174 178 101 135
##  [91] 178 182 105 139 182 186 109 143 186 190
x + y
## Error in x + y: non-numeric argument to binary operator

Accessing particular values

x[2]
## [1] "banana"
y[2:3]
## [1] 45 87
z[ z > 23 & z < 56]
##  [1] 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
## [26] 49 50 51 52 53 54 55

Functions

round(pi, digits = 4)
## [1] 3.1416

factors

x <- c("low", "high", "medium", "high", "low", "medium", "high")
response <- factor(x)
response
## [1] low    high   medium high   low    medium high  
## Levels: high low medium
response <- factor(x, levels = c("low", "medium", "high"))
response
## [1] low    high   medium high   low    medium high  
## Levels: low medium high

matrices

y <- matrix(data = 1:12, nrow = 4, ncol = 3)
dim(y)
## [1] 4 3
ncol(y)
## [1] 3
nrow(y)
## [1] 4
y[2, 3]
## [1] 10
y[2:3, ]
##      [,1] [,2] [,3]
## [1,]    2    6   10
## [2,]    3    7   11

Lists

cities <- list(
    city = c("Kampala", "London", "Paris"),
    population = c(1.51, 8.8, 2.1)
)

cities
## $city
## [1] "Kampala" "London"  "Paris"  
## 
## $population
## [1] 1.51 8.80 2.10

Accessing list elements

cities[[2]]
## [1] 1.51 8.80 2.10
cities$city
## [1] "Kampala" "London"  "Paris"

Modifying the list

Modify a specific entr

cities$city[2] <- "New York"

Add a new element

cities$country <- c("Uganda", "USA", "France")
cities
## $city
## [1] "Kampala"  "New York" "Paris"   
## 
## $population
## [1] 1.51 8.80 2.10
## 
## $country
## [1] "Uganda" "USA"    "France"

Data Frames

dat <- data.frame(
    city = c("Kampala", "London", "Paris"),
    population = c(1.51, 8.8, 2.1)
)
dat
##      city population
## 1 Kampala       1.51
## 2  London       8.80
## 3   Paris       2.10

Accessing records

dat[1:2, ]
##      city population
## 1 Kampala       1.51
## 2  London       8.80
dat$city[2]
## [1] "London"
dat[dat$population > 2, "city"]
## [1] "London" "Paris"